package cn.changeforyou.tcpIp.analysis.demo.protocol.gbt32960_3.handler;

import cn.changeforyou.tcpIp.analysis.demo.datastructs.HexStringReadBuffer;
import cn.changeforyou.tcpIp.analysis.demo.protocol.gbt32960_3.BusUploadProtocol;
import cn.changeforyou.tcpIp.analysis.demo.protocol.gbt32960_3.TotalCarInfo;
import cn.changeforyou.tcpIp.analysis.demo.protocol.gbt32960_3.enums.InfoSign;
import cn.changeforyou.tcpIp.analysis.demo.protocol.gbt32960_3.RealtimeInfoUpload;
import cn.changeforyou.tcpIp.analysis.demo.protocol.gbt32960_3.enums.InfoStruts;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 实时信息上传处理器
 * @author zhyu
 * @date 2020/8/7 15:28
 */
public class RealtimeInfoUploadHandler {

    public void analysis(BusUploadProtocol protocol) {
        HexStringReadBuffer buffer = new HexStringReadBuffer(protocol.getDataUnitChars());
        LocalDateTime localDateTime = buffer.readDatetime();
        RealtimeInfoUpload upload = new RealtimeInfoUpload();
        upload.setDateTime(localDateTime);
        //先假定必有
        List<InfoStruts> infos = upload.getInfoStruts();
        boolean notOk = false;
        if(buffer.avialable()) {
            notOk = true;
        }
        while(notOk) {
            String sign = buffer.readHexString(2);
            System.out.println("信息类型标识为: " + sign);
            InfoSign infoSign = InfoSign.getByWord(sign);
            InfoStruts infoStruts = new InfoStruts();
            infoStruts.setSign(infoSign);
            switch (infoSign) {
                case total_car_info:
                    TotalCarInfo info = parseTotolCarInfo(buffer);
                    infoStruts.setBody(info);
                    infos.add(infoStruts);
                    break;
            }
            if(buffer.avialable()) {
                notOk = true;
            }
        }
    }


    public TotalCarInfo parseTotolCarInfo(HexStringReadBuffer buffer) {
        TotalCarInfo info = new TotalCarInfo();
        info.setCar_status(buffer.readHexString(2));
        info.setRecharge_status(buffer.readHexString(2));
        info.setRuntime_status(buffer.readHexString(2));
        info.setCar_speed(buffer.readShortExcept("FFFE", "FFFF"));
        info.setTotol_mileage(buffer.readIntExcept("FFFFFFFFE", "FFFFFFFF"));
        info.setTotal_voltage(buffer.readShortExcept("FFFE", "FFFF"));
        info.setTotal_electricity(buffer.readShortExcept("FFFE", "FFFF"));
        info.setSoc(buffer.readByteExcept("FE", "FF"));
        info.setDc_status(buffer.readByteExcept("FE", "FF"));
        info.setShift(buffer.readByteExcept("FE", "FF"));
        info.setResistance(buffer.readShortExcept("FFFE", "FFFF"));
        info.setReserved(buffer.readByteExcept("FE", "FF"));
        return info;
    }
}
